package org.example.demo.broker;

import com.google.common.collect.Lists;
import org.example.demo.domain.Message;

import java.util.ArrayList;
import java.util.List;


public class MessageHolder {
    private static final ThreadLocal<List<Message>> HOLDER = ThreadLocal.withInitial(ArrayList::new);

    public static void add(Message message) {
        HOLDER.get().add(message);
    }

    public static List<Message> getAndClear() {
        List<Message> tmp = Lists.newArrayList(HOLDER.get());
        HOLDER.remove();
        return tmp;
    }
}
